package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 获取章节
 * Created by yvdedu.com on 2016/9/27.
 */
@WebServlet(urlPatterns = "/search_chapter")
public class search_chapter extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        String user_id=req.getParameter("user_id");
        String status=req.getParameter("status");
        MySql mySql=new MySql();
        if(status.equals("buy")){
            /*List<Chapter> chapters=mySql.getBuyChapterByUserId(Integer.parseInt(user_id));
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(chapters);
            resp.getWriter().print(json);*/
            req.setAttribute("buy_book",mySql.getBuyChapterByUserId(Integer.parseInt(user_id)));
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }
        if(status.equals("collect")){
            req.setAttribute("collect_book",mySql.getCollectBookByUserId(Integer.parseInt(user_id)));
            req.getRequestDispatcher("index.jsp").forward(req,resp);
            /*List<Chapter> chapters=mySql.getCollectChapterByUserId(Integer.parseInt(user_id));
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(chapters);
            resp.getWriter().print(json);*/
        }
        if(status.equals("history")){
            req.setAttribute("history_book",mySql.getHisChapterByUserId(Integer.parseInt(user_id)));
            req.getRequestDispatcher("index.jsp").forward(req,resp);
            /*List<Chapter> chapters=mySql.getHisChapterByUserId(Integer.parseInt(user_id));
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(chapters);
            resp.getWriter().print(json);*/
        }
    }
}
